/*
 * #%L
 * Server
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.ractoc.opengamefinder.server;

import java.io.File;
import java.sql.DriverManager;
import org.apache.log4j.Logger;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ractoc
 */
public class ShutDownServer extends Thread {

    private static Logger logger = Logger.getLogger(OGFServerStartup.class.getName());

    @Override
    public void run() {
        try {
            logger.warn("shutting down server");
            String customConfig = ConfigLocator.getCustomConfig("hibernate.cfg.xml");
            Configuration config = null;
            if (customConfig != null) {
                config = new AnnotationConfiguration().configure(new File(customConfig));
            } else {
                config = new AnnotationConfiguration().configure("hibernate.cfg.xml");
            }
            logger.debug("connection.driver_class: " + config.getProperty("connection.driver_class"));
            if (config.getProperty("connection.driver_class").contains("derby")) {
                Class.forName(config.getProperty("connection.driver_class")).newInstance();
                logger.debug("connection.url: " + config.getProperty("connection.url"));
                DriverManager.getConnection(config.getProperty("connection.url") + ";shutdown=true");
            }
        } catch (Exception ee) {
            if (!ee.getMessage().startsWith("java.sql.SQLNonTransientConnectionException")) {
                logger.error(ee);
            }
        }
    }
}
